Ext.define('Module', {
    extend: 'Ext.data.Model',
    fields: [
       {name: 'name',type: 'string'},
       {name: 'url', type: 'string'},
       {name:'level',type:'number'},
       {name:'sort',type:'number'}
    ]        
});

var superModuleStore = Ext.create('Ext.data.TreeStore', {
	fields: ['id','name'],
    proxy: {
    	type : 'ajax',
		url:'moduleAction!getTreeBypid.action'
    },
    root: {
    	id:0,
        name: '根节点',
        expanded: true
      },
    reader : {
		type : 'json'
	}
});

//数据列表store
var store = Ext.create('Ext.data.TreeStore', {
    model: 'Module',
    proxy: {
    	type: 'ajax',
        url: 'moduleAction!queryTree.action',
        reader: {
            type: 'json',
            root: 'list'
        }
    }
});

var ikeaForm=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
    border:0,
    url:'moduleAction!saveOrUpdate.action',
    layout: 'anchor',
    defaults: {
        anchor: '100%'
    },
    defaultType: 'textfield',
    defaults: {
        anchor: '90%',
        labelAlign:'left'
      },
    items: [{
            xtype: 'hiddenfield',
            name: 'module.id'
        },
         {
            xtype: 'hiddenfield',
            name: 'module.pid'
        },
       {
          fieldLabel: '模块名称<font color=red>*</font>',
          name: 'module.name',
          allowBlank: false
      },{
          id: 'module.tempid',
          fieldLabel: '上级模块',
          xtype: 'treepicker',
          store: superModuleStore,
          minPickerHeight: 200,
          displayField: 'name',
          valueField: 'id',
//          autoScroll:true,
          overflowY:'auto',
          overflowX:'auto',
          listeners:{
            blur:function(cmp,the,opts){
//              superModuleStore.reload();
            }
          }
        },{
          fieldLabel: 'URL',
          name: 'module.url'
      }, {
            xtype      : 'fieldcontainer',
            fieldLabel : '是否叶子节点',
            defaultType: 'radiofield',
            layout: 'hbox',
            items: [
                {
                    boxLabel  : '是',
                    name      : 'module.leaf',//是叶子节点
                    inputValue: 'true',
                    checked:true
                }, {
                    boxLabel  : '否',
                    name      : 'module.leaf',
                    inputValue: 'false'
                }
            ]
        },
        {
        xtype: 'displayfield',
        fieldLabel: '级别',
        name: 'module.level',
        value: '<i>提交之后自动改变</i>'
    },{
          fieldLabel: '排序',
          name: 'module.sort'
      }]
});

function refreshCertainNode(){
  var form=ikeaForm.getForm();
  var pid=form.findField('module.pid').getValue();
  var refreshNode=store.getRootNode();
  store.load({node:refreshNode});
}

var ikeaWin=Ext.create('Ext.window.Window', {
    title: '新增',
    height: 280,
    width: 400,
    modal:true,
    layout: 'fit',
    closeAction:'hide',
    closable : true,
    items:[ikeaForm],
    listeners:{
      show:function(){
        ikeaForm.getForm().reset();
        superModuleStore.reload();
      }
    },
    buttons: [{
        text: '提交',
        formBind: true, //only enabled once the form is valid
        handler: function() {

            var form = ikeaForm.getForm();
              var tempid=form.findField('module.tempid').getValue();
              console.log('tempid');
              form.findField('module.pid').setValue(tempid);
               form.submit({
                    success: function(form, action) {
                      //console.log(action.result);
                      var msg="新增成功！";
                      var id=form.findField('module.id').getValue();
                      if(id){
                        msg="修改成功！";
                      }
                      Ext.Msg.show({
                      title: '提示',
                      msg: msg,
                      buttons: Ext.Msg.OK,
                      width: 300,
                      fn: function(){
                             refreshCertainNode();
                       }
                     });
                      ikeaWin.hide();
                    }
                });
            }
        },{
            text : '取消',
            handler : function() {
            	ikeaWin.close();
            }
        }
    ],
    buttonAlign:'center'
});

/**
 * 树形列表
 */
var grid = Ext.create('Ext.tree.Panel', {
    store: store,
    useArrows: true,
    root : {
		id : 0,
		name : "根目录",
		expanded : true
	},
    rootVisible: false,
    border:0,
    listeners:{
      itemclick:function(view,record,item,index,event,opts){
      }
    },
    columns: [{
        xtype: 'treecolumn',
        text: '名称',
        sortable: true,
        width:200,
        dataIndex: 'name'
    },{
        text: '级别',
        sortable: true,
        width:200,
        dataIndex: 'level'
    },
    {
        text: '排序',
        sortable: true,
        width:200,
        dataIndex: 'sort'
    },
      {
        text: '路径',
        sortable: true,
        dataIndex: 'url',
        flex: 1
    }],
    tbar: [{
    	xtype: 'button', 
    	text: '新增' ,
    	iconCls: 'icon-add',
    	handler:function(){
           ikeaWin.setTitle('新增');
    	   ikeaWin.show();
    	}},
        { 
		xtype: 'button', 
		text: '修改' ,
		iconCls: 'icon-edit',
		handler:function(){
			var selMode = grid.getSelectionModel();
			var model=selMode.getLastSelected();
	        if(!model){
	             Ext.Msg.alert('提示','请选择一条数据！');
	             return;
	        }
            var raw=model.raw;
        	if(raw){
			   ikeaWin.setTitle('修改');
			   ikeaWin.show();
			   ikeaForm.getForm().findField('module.id').setValue(raw.id);
			   ikeaForm.getForm().findField('module.pid').setValue(raw.pid);
	           ikeaForm.getForm().findField('module.tempid').setValue(raw.pid);
			   ikeaForm.getForm().findField('module.name').setValue(raw.name);
			   ikeaForm.getForm().findField('module.url').setValue(raw.url);
	           ikeaForm.getForm().findField('module.leaf').setValue(raw.leaf);
	           ikeaForm.getForm().findField('module.level').setValue(raw.level);
	           ikeaForm.getForm().findField('module.sort').setValue(raw.sort);
        	}else{
        		   Ext.Msg.alert('提示','请选择要修改的数据行！');
        	}
           }}
         ]
});

Ext.onReady(function(){
    Ext.create('Ext.container.Viewport', {
        layout: 'fit',
        items: [grid]
    });
});
